﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DZPK
{
    public class CommonFunction
    {
        public static char HexToChar(char hex)
        {
            char ret = '\0';

            if (hex >= 0 && hex <= 9)
            {
                ret = (char)(hex + '0');
            }
            else if (hex >= 10 && hex <= 15)
            {
                ret = (char)(hex - 10 + 'a');
            }
            else 
            {
                throw new Exception("HexToChar error.");
            }

            return ret;
        }

        public static char[] ByteArrayToCharArray(byte[] bytes, int index, int len)
        {
            char[] ret = new char[len * 2];

            for (int i = index; i < index + len; i++)
            {
                byte low = (byte)(bytes[i] & 0x0F);
                byte high = (byte)(bytes[i] & 0xF0);
                high = (byte)(high >> (byte)4);

                ret[2 * i] = HexToChar((char)high);
                ret[2 * i + 1] = HexToChar((char)low);
            }

            return ret;
        }
    }
}
